using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace eBirdAttend
{
    public partial class Department : UserControl
    {
        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();
        String strBtnCaption = "";
        

        public Department()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
           
        }


        // Function name   : btnAdd_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnAdd_Click(object sender, EventArgs e)
        {   
            ClearControls();
            strBtnCaption = "Add";
            groupBox1.Text = "ADD A DEPARTMENT";
         
            sqlCmd.CommandText = "select max(depid) from department";
            sqlCmd.Connection = sqlCon;
            sqlCon.Open();
            SqlDataReader sqlReader;
            sqlReader = sqlCmd.ExecuteReader();
            if (sqlReader.Read())
            {
                if (sqlReader[0].ToString() == "")
                {
                }
                else
                {
                    txtDeptId.Text = Convert.ToString(Convert.ToInt32(sqlReader[0].ToString()) + 1);
                }
            }
            else
            {
                txtDeptId.Text = "1";
            }

            if (txtDeptId.Text == "")
            {
                txtDeptId.Text = "1";
            }
            
            sqlCon.Close();          
            groupBox1.Visible = true;
            txtDeptName.Focus();       
                     
        }


        // Function name   : btnEdit_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnEdit_Click(object sender, EventArgs e)
        {   
            ClearControls();
            if (deptView.SelectedRows.Count == 0)
            { MessageBox.Show("Please select a row for edit"); return; }

            if ((deptView.RowCount - 1) == deptView.SelectedRows[0].Index)
            {
                MessageBox.Show("No content to edit");
                groupBox1.Visible = false;
                ClearControls();
                return;
            }
            groupBox1.Visible = true;
            strBtnCaption = "Edit";
            groupBox1.Text = " EDIT A DEPARTMENT ";
            txtDeptName.Focus();        
           
            //MessageBox.Show(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());           
            txtDeptId.Text = deptView.SelectedRows[0].Cells[1].Value.ToString();
            txtDeptName.Text = deptView.SelectedRows[0].Cells[2].Value.ToString();
            txtDeptHod.Text = deptView.SelectedRows[0].Cells[3].Value.ToString();
            
        }


        // Function name   : btnDelete_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (deptView.SelectedRows.Count == 0)
                { MessageBox.Show("Please select a row for delete"); return; }

                DialogResult _dlg = MessageBox.Show("Are you sure you want to delete?", "Warning", MessageBoxButtons.YesNo);
                if (_dlg == DialogResult.No)
                {
                    return;
                }
                else
                {
                    string strDepID = deptView.SelectedRows[0].Cells[1].Value.ToString();
                    while (deptView.Rows.Count > 1)
                        deptView.Rows.RemoveAt(0);
                    sqlCmd.CommandText = "delete from  Department where DepId ='" + strDepID + "'";
                    sqlCmd.Connection = sqlCon;
                    sqlCon.Open();
                    sqlCmd.ExecuteNonQuery();
                    sqlCon.Close();
                    departmentTableAdapter.Fill(DepartmentDataSet1.Department);

                    MessageBox.Show("Successfuly Deleted!!!");
                }
            }
            catch (Exception ex)
            {
            }
        }


        // Function name   : btnSave_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtDeptName.Text == "")
            {
                MessageBox.Show("Enter the department name");
                txtDeptName.Focus();
            }
            else
            {
                try
                {
                    //save the new department details
                    if (strBtnCaption == "Add")
                    {
                        while (deptView.Rows.Count > 1)
                            deptView.Rows.RemoveAt(0);
                        sqlCmd.CommandText = "insert into Department (DepCmpId,DepName,DepHod) values('" + "1" + "','" + txtDeptName.Text + "','" + txtDeptHod.Text + "')";
                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();

                        departmentTableAdapter.Fill(DepartmentDataSet1.Department);
                        deptView.Refresh();
                        MessageBox.Show("Successfuly Inserted");
                    }
                    else
                    {
                        //update the department details
                        while (deptView.Rows.Count > 1)
                            deptView.Rows.RemoveAt(0);

                        sqlCmd.CommandText = "update  Department set DepCmpId ='" + "1" + "',DepName='" + txtDeptName.Text + "',DepHod='" + txtDeptHod.Text + "' where DepId='" + txtDeptId.Text + "'";
                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();

                        departmentTableAdapter.Fill(DepartmentDataSet1.Department);

                        MessageBox.Show("Successfuly Updated");
                    }
                }
                catch (Exception e1)
                {
                }
                finally
                {
                    sqlCon.Close();
                    ClearControls();
                }
            }
        }       


        // Function name   : ClearControls
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        
        public void ClearControls()
        {
            txtDeptId.Text = "";
            txtDeptName.Text = "";
            txtDeptHod.Text = "";
            groupBox1.Visible = false;
        }


        // Function name   : btnCancel_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            btnEdit.Visible = true;
            ClearControls();
        }

        private void Department_Load(object sender, EventArgs e)
        {
            departmentTableAdapter.Fill(DepartmentDataSet1.Department);
            deptView.Columns[0].Visible = false;
            deptView.Columns[1].Visible = false;
            deptView.Columns[2].Width = 150;
            deptView.Columns[3].Width = 225;
        }


        // Function name   : btnClose_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/19/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}
